﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class User_FavoriteList : System.Web.UI.Page
{
    UsersRepository URepo = new UsersRepository();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            HttpCookie _user = Request.Cookies["userinfo"];
            if (Request.Cookies["userinfo"] != null)
            {
                if (_user["username"] != null)
                {
                    int userId = URepo.GetUserIdByName(_user["username"]);
                    GetFavorites(userId);
                    hdfCurrentUI.Value = userId.ToString();
                }
                else
                    Response.Redirect(ResolveUrl("~/Index.html"));
            }
            else
                Response.Redirect(ResolveUrl("~/Index.html"));
        }
    }
    private void GetFavorites(int UserId)
    {
        try
        {
            var a = URepo.GetFavoritesByUserId(UserId);
            if (a != null)
            {
                grvFavorites.DataSource = a;
                grvFavorites.DataBind();
            }
        }
        catch
        {
        }
    }

    protected void grvFavorites_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grvFavorites.PageIndex = e.NewPageIndex;
        if (!string.IsNullOrEmpty(hdfCurrentUI.Value))
        {
            GetFavorites(int.Parse(hdfCurrentUI.Value));
        }
        else
        {
            HttpCookie _user = Request.Cookies["userinfo"];
            if (Request.Cookies["userinfo"] != null)
            {
                if (_user["username"] != null)
                {
                    int UserId = URepo.GetUserIdByName(_user["username"]);
                    GetFavorites(UserId);
                }
            }
            
        }
    }

    protected void grvFavorites_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "delete")
        {
            try
            {
                int Id = int.Parse(e.CommandArgument.ToString());
                URepo.DeteleFavorite(Id);
                if (!string.IsNullOrEmpty(hdfCurrentUI.Value))
                {
                    GetFavorites(int.Parse(hdfCurrentUI.Value));
                }
                else
                {
                    HttpCookie _user = Request.Cookies["userinfo"];
                    if (Request.Cookies["userinfo"] != null)
                    {
                        if (_user["username"] != null)
                        {
                            int UserId = URepo.GetUserIdByName(_user["username"]);
                            GetFavorites(UserId);
                        }
                    }
                }
            }
            catch
            {
            }
        }
    }
}